package com.ruoyi.web.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.web.domain.entity.Student;
import com.ruoyi.web.domain.entity.Teacher;
import com.ruoyi.web.domain.entity.Topic;
import com.ruoyi.web.domain.vo.TopicDetailVo;
import com.ruoyi.web.domain.vo.TopicInfoVo;
import com.ruoyi.web.mapper.StudentMapper;
import com.ruoyi.web.mapper.TopicMapper;
import com.ruoyi.web.service.teacher.topicInfo.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 选题信息表(TopicTable)表服务实现类
 *
 * @author makejava
 * @since 2022-10-29 16:32:36
 */
@Service
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements TopicService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TopicMapper topicMapper;

    @Override
    public R getTopicList(Integer pageNum, Integer pageSize) {
        //封装分页条件
        IPage<Topic> page = new Page<>(pageNum, pageSize);

        topicMapper.selectPage(page, null);

        //查询课题列表
        List<Topic> topics = page.getRecords();

        //根据学生id查课题信息并将课题部分信息取出
        List<Student> studentList = new ArrayList<>();
        for (Topic topic : topics) {
            Student student = studentMapper.getByStuId(topic.getSubmitId());
            student.setTopic(topic.getTopic());
            student.setTopicDataState(topic.getDataState());
            Teacher teacher = studentMapper.getByTeaId(student.getInstructor1());
            student.setTeaName(teacher.getTeaName());
            student.setTeaJobnum(teacher.getTeaJobnum());
            studentList.add(student);
        }


        //dao转vo
        List<TopicInfoVo> topicInfoVoList = BeanUtils.copyBeanList(studentList, TopicInfoVo.class);

        //TODO 封装分页信息对象返回给前端
        return R.ok(topicInfoVoList);

    }

    @Override
    public R getTopicInfoById(Long stuId, Long instructor2) {
        Topic topic = topicMapper.selectById(stuId);

        //当该学生没有第二指导教师不需要去查
        if ( instructor2 != null && instructor2.longValue() > 0) {
            topic.setTeaName(studentMapper.getByTeaId(instructor2).getTeaName());
        }

        TopicDetailVo topicDetailVo = BeanUtils.copyBean(topic, TopicDetailVo.class);

        return R.ok(topicDetailVo);
    }
}

